//
//  DXVisitorView.swift
//  新浪微博
//
//  Created by dix on 2017/1/18.
//  Copyright © 2017年 dix. All rights reserved.
//

import UIKit

class DXVisitorView: UIView {
    //定义一个闭包
    var closure:(()->())?
    override init(frame : CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   private func setUpUI(){
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        //添加控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registButton)
        //02 添加约束
        feedImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.width.equalTo(230)
            make.centerX.equalTo(self)
        }
        loginButton.snp_makeConstraints { (make) in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        registButton.snp_makeConstraints { (make) in
            make.size.equalTo(loginButton)
            make.top.equalTo(loginButton)
            make.right.equalTo(messageLabel)
        }
        
    }
    // MARK: - 懒加载控件
    //圆圈图片
    private lazy var feedImageView : UIImageView = UIImageView(imgName : "visitordiscover_feed_image_smallicon")
    //遮挡
    private lazy var maskImageView : UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    //icon
    private lazy var iconImageView : UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    
    lazy var messageLabel : UILabel = {
        let lab = UILabel()
        //字体大小
        lab.font = UIFont.systemFont(ofSize: 14)
        lab.textColor = UIColor.darkGray
        //设置text
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //换行
        lab.numberOfLines = 0
        lab.textAlignment = .center
        return lab
    }()
    //登录按钮
    private lazy var loginButton : UIButton = {
        let btn = UIButton()
        //点击事件
        btn.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named : "common_button_white"), for: UIControlState.normal)
        //设置文字
        btn.setTitle("登录", for: UIControlState.normal)
        //设置文字颜色
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(DXThemeColor, for: UIControlState.highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return btn
    }()
    //注册按钮
    private lazy var registButton : UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named : "common_button_white"), for: UIControlState.normal)
        //设置文字
        btn.setTitle("注册", for: UIControlState.normal)
        //设置文字颜色
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(DXThemeColor, for: UIControlState.highlighted)
        //设置文字大小
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return btn
    }()
    
   @objc private func buttonClick(){
        closure?()
    
    }
    // MARK: - 供外界设置的方法
    func setUpVisiorViewInfo(imgName : String?, title : String?){
        //代表是消息 发现 我
        if let img = imgName, let tit = title {
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            //隐藏圆圈图片
            feedImageView.isHidden = true
        }else{
            //代表是首页
            //执行动画
            setUpFeedImageViewAnim()
        
        }
    }
    private func setUpFeedImageViewAnim(){
        //核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置toValue
        anim.toValue = M_PI * 2
        //设置时长
        anim.duration = 20
        //重复次数
        anim.repeatCount = MAXFLOAT
        // 当切换控制器 或者程序退到后台 默认动画会被移除
        anim.isRemovedOnCompletion = false
        feedImageView.layer.add(anim, forKey: nil)
    
    }
    
}
